<?php
function tf_get_elements() {
  global $thfr_css, $aElements;

  $aElements['sitetitle_item'] = '
  <h1 id="sitetitle"><a href="<?php echo home_url(); ?>/"><?php bloginfo("name"); ?></a></h1>
  ';
  $aElements['tagline_item'] = '
  <p id="tagline"><?php bloginfo("description"); ?></p>
  ';
  $aElements['hormenu_item'] = '<nav>
  <?php
  wp_nav_menu( array(
  "theme_location" => "menu1",
  "container" => "div",
  "container_id" => "hormenu",
  "container_class" => "clearfix",
  "menu_id" => "nav",
  "fallback_cb" => "tf_pagemenu",
  ) );
  ?>
  </nav>';
  $aElements['hormenu2_item'] = '<nav>
  <?php
  wp_nav_menu( array(
  "theme_location" => "menu2",
  "container" => "div",
  "container_id" => "hormenu2",
  "container_class" => "clearfix",
  "menu_id" => "nav2",
  "fallback_cb" => "tf_catmenu",
  ) );
  ?>
  </nav>';
  $aElements['headerimage_item'] = '
  <div class="headerimage"></div>
  ';
  $aElements['breadcrumb_item'] = '
  <?php if ( function_exists("my_breadcrumbs") ) { ?>
  <div class="breadcrumbs" id="breadcrumbs1">
  <?php my_breadcrumbs(); ?>
  </div>
  <?php } ?>
  ';
  $aElements['breadcrumb2_item'] = '
  <?php if ( function_exists("my_breadcrumbs") ) { ?>
  <div class="breadcrumbs" id="breadcrumbs2">
  <?php my_breadcrumbs(); ?>
  </div>
  <?php } ?>
  ';
  $aElements['pagetitle_item'] = $thfr_css['multipagetitles'];
  $aElements['multinav_item'] = '
  <div class="multinav clearfix" id="multinav1">
  <div class="older">
  ' . $thfr_css['nextprevnav']['multi-left'] . '
  </div>
  <div class="newer">
  ' . $thfr_css['nextprevnav']['multi-right'] . '
  </div>
  </div>
  ';
  $aElements['multinav2_item'] = '
  <div class="multinav clearfix" id="multinav2">
  <div class="older">
  ' . $thfr_css['nextprevnav']['multi2-left'] . '
  </div>
  <div class="newer">
  ' . $thfr_css['nextprevnav']['multi2-right'] . '
  </div>
  </div>
  ';
  $aElements['singlenav_item'] = '
  <div class="singlenav clearfix" id="singlenav1">
  <div class="older">
  ' . $thfr_css['nextprevnav']['single-left'] . '
  </div>
  <div class="newer">
  ' . $thfr_css['nextprevnav']['single-right'] . '
  </div>
  </div>
  ';
  $aElements['singlenav2_item'] = '
  <div class="singlenav clearfix" id="singlenav2">
  <div class="older">
  ' . $thfr_css['nextprevnav']['single2-left'] . '
  </div>
  <div class="newer">
  ' . $thfr_css['nextprevnav']['single2-right'] . '
  </div>
  </div>
  ';
  $aElements['searchform_item'] = '
  <section>
  <div class="searchform">
  <form method="get">
  <input class="searchfield" type="search" name="s" value="" placeholder="поиск..." />
  <input class="searchbutton" type="submit" name="submit" value="Найти" />
  </form>
  </div>
  </section>
  ';
  $aElements['rsslink_item'] = '
  <a href="<?php bloginfo("rss2_url"); ?>" class="rsslink">RSS подписка</a>
  ';
  $aElements['rsscommentslink_item'] = '
  <a href="<?php bloginfo("comments_rss2_url"); ?>" class="rsscommentslink">Пожписаться на комментарии</a>
  ';
  $aElements['feedburner_item'] = '
  <a href="#" class="feedburnerlink">RSS по Email</a>
  ';
  $aElements['twitter_item'] = '
  <a href="#" class="twitterlink">Twitter</a>
  ';
  $aElements['facebook_item'] = '
  <a href="#" class="facebooklink">Facebook</a>
  ';
  $aElements['footer_item'] = '
  <footer><div id="div-footer">' . $thfr_css['footercontent'] . '</div></footer>
  ';
  // Widget Areas
  $aElements['widgetarea_1'] = '
  <div class="widgetarea" id="widgetarea1">
  <?php dynamic_sidebar("WidgetArea-1"); ?>
  </div>';
  $aElements['widgetarea_1_2'] = '
  <div class="widgetarea" id="widgetarea1-2">
  <?php dynamic_sidebar("WidgetArea-1-2"); ?>
  </div>';
  $aElements['widgetarea_1_3'] = '
  <div class="widgetarea" id="widgetarea1-3">
  <?php dynamic_sidebar("WidgetArea-1-3"); ?>
  </div>';
  $aElements['widgetarea_2'] = '
  <div class="widgetarea" id="widgetarea2">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" />
  </colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-2 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-2 Col-2"); ?>
  </td>
  </tr>
  </table>
  </div>';
  $aElements['widgetarea_2_2'] = '
  <div class="widgetarea" id="widgetarea2-2">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" />
  </colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-2-2 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-2-2 Col-2"); ?>
  </td>
  </tr>
  </table>
  </div>';
  $aElements['widgetarea_3'] = '
  <div class="widgetarea" id="widgetarea3">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" /><col class="col3" /></colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-3 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-3 Col-2"); ?>
  </td>
  <td class="td3">
  <?php dynamic_sidebar("WidgetArea-3 Col-3"); ?>
  </td>
  </tr>
  </table>
  </div>';
  $aElements['widgetarea_4'] = '
  <div class="widgetarea" id="widgetarea4">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" />
  <col class="col3" /><col class="col4" />
  </colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-4 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-4 Col-2"); ?>
  </td>
  <td class="td3">
  <?php dynamic_sidebar("WidgetArea-4 Col-3"); ?>
  </td>
  <td class="td4">
  <?php dynamic_sidebar("WidgetArea-4 Col-4"); ?>
  </td>
  </tr>
  </table>
  </div>';
  $aElements['widgetarea_5'] = '
  <div class="widgetarea" id="widgetarea5">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" />
  <col class="col3" /><col class="col4" /><col class="col5" />
  </colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-5 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-5 Col-2"); ?>
  </td>
  <td class="td3">
  <?php dynamic_sidebar("WidgetArea-5 Col-3"); ?>
  </td>
  <td class="td4">
  <?php dynamic_sidebar("WidgetArea-5 Col-4"); ?>
  </td>
  <td class="td5">
  <?php dynamic_sidebar("WidgetArea-5 Col-5"); ?>
  </td>
  </tr>
  </table>
  </div>';
  $aElements['widgetarea_6'] = '
  <div class="widgetarea" id="widgetarea6">
  <table style="table-layout:fixed" width="100%" cellpadding="0" cellspacing="0">
  <colgroup><col class="col1" /><col class="col2" />
  <col class="col3" /><col class="col4" />
  <col class="col5" /><col class="col6" />
  </colgroup>
  <tr>
  <td class="td1">
  <?php dynamic_sidebar("WidgetArea-6 Col-1"); ?>
  </td>
  <td class="td2">
  <?php dynamic_sidebar("WidgetArea-6 Col-2"); ?>
  </td>
  <td class="td3">
  <?php dynamic_sidebar("WidgetArea-6 Col-3"); ?>
  </td>
  <td class="td4">
  <?php dynamic_sidebar("WidgetArea-6 Col-4"); ?>
  </td>
  <td class="td5">
  <?php dynamic_sidebar("WidgetArea-6 Col-5"); ?>
  </td>
  <td class="td6">
  <?php dynamic_sidebar("WidgetArea-6 Col-6"); ?>
  </td>
  </tr>
  </table>
  </div>';

  // Custom drop images:
  if (is_array($thfr_css['customdropimages']) and !empty($thfr_css['customdropimages'])) {
    foreach ($thfr_css['customdropimages'] as $newimageid => $img) {
      $sCustomImage = '';
      if ($img['link'] == 'on') {
        if ($img['linktype'] == 'custom' and $img['customurl'] != '') {
          $url = $img['customurl'];
        }
        else
        {
          $url = '<?php echo home_url( "/" ); ?>';
        }
        if ($img['linktitle'] != '') {
          $title = ' title="' . $img['linktitle'] . '"';
        }
        else
        {
          $title = '';
        }
        if ($img['linktarget'] != '') {
          $target = ' target="' . $img['linktarget'] . '"';
        }
        else
        {
          $target = '';
        }
        if ($img['linkrel'] != '') {
          $rel = ' rel="' . $img['linkrel'] . '"';
        }
        else
        {
          $rel = '';
        }

        $linkid = str_replace('.', '', $newimageid) . '-link';
        $sCustomImage .= '<a id="' . $linkid . '" href="' . $url . '"' . $title . $target . $rel . '>';
      }

      if ($img['alt'] == 'on') {
        $alt = ' alt="' . $img['alttext'] . '"';
      }
      else
      {
        $alt = '';
      }
      $sCustomImage .= '<img id="' . str_replace('.', '', $newimageid) . '" src="<?php bloginfo("template_url"); ?>/images/' . $img['file'] . '"' . $alt . ' />';

      if ($img['link'] == 'on') {
        $sCustomImage .= '</a>';
      }

      $dropitemname = str_replace('.', '', $newimageid) . '_custimg';
      $aElements[$dropitemname] = $sCustomImage;
    }
  }
  // End cust img

  // Custom drop links:
  if (is_array($thfr_css['customdroplinks']) and !empty($thfr_css['customdroplinks'])) {
    foreach ($thfr_css['customdroplinks'] as $linkid => $linkinfo) {
      $sCustomLink = '<a id="' . $linkid . '" href="' . $linkinfo['linkurl'] . '"';
      if ($linkinfo['linktitle'] != '') {
        $sCustomLink .= ' title="' . $linkinfo['linktitle'] . '"';
      }
      if ($linkinfo['linktarget'] != '') {
        $sCustomLink .= ' target="' . $linkinfo['linktarget'] . '"';
      }
      if ($linkinfo['linkrel'] != '') {
        $sCustomLink .= ' rel="' . $linkinfo['linkrel'] . '"';
      }
      $sCustomLink .= '>' . $linkinfo['linktext'] . '</a>';

      $dropitemname = $linkid . '_custlink';
      $aElements[$dropitemname] = $sCustomLink;
    }
  }
  // End cust links

  // Custom drop divs:
  if (is_array($thfr_css['customdropdivs']) and !empty($thfr_css['customdropdivs'])) {
    foreach ($thfr_css['customdropdivs'] as $divid => $divinfo) {
      $sCustomDiv = '<div id="' . $divid . '">';
      if ($divinfo['content-real'] != '') {
        $sCustomDiv .= $divinfo['content-real'];
      }
      $sCustomDiv .= '</div>';

      $dropitemname = $divid . '_custdiv';
      $aElements[$dropitemname] = $sCustomDiv;
    }
  }
  // End cust divs
  return $aElements;
}

function tf_create_php_file($thisTemplate) {
  global $thfr_css, $aElements;

  tf_get_elements();

  $output_string = '';
  if ($thisTemplate == 'comments') {
    // comments.php
    $output_string .= tf_create_php_file_comments();
  } else {
    //include header.php
    $output_string .= '<?php include("header.php"); ?>';

    // bodytag insert
    $output_string .= tf_create_php_file_bodytag($thisTemplate);
    // bodytop insert
    $output_string .= tf_create_php_file_bodytop($thisTemplate);

    $output_string .= tf_create_php_file_layout($thisTemplate);

    //include footer.php
    $output_string .= '<?php include("footer.php"); ?>';
  }

  return $output_string;
}

function tf_create_php_file_layout($thisTemplate) {
  if ($thisTemplate == 'index') {
    $content = "<?php get_template_part('loop'); ?>";
  } else {
    $content = tf_create_php_file_content($thisTemplate);
  }
  $aSelectors = array(
    'content' => $content,
    'left' => tf_create_php_file_left($thisTemplate),
    'right' => tf_create_php_file_right($thisTemplate),
    'header' => tf_create_php_file_header($thisTemplate),
    'footer' => tf_create_php_file_footer($thisTemplate),
  );
  return tf_common_template_layout($thisTemplate, $aSelectors);
}

function tf_create_php_file_bodytop($thisTemplate) {
  global $thfr_css;

  $output_string = '';
  if ($thfr_css['codeinserts']['bodytop'][$thisTemplate . '-global'] == 'on' and $thfr_css['codeinserts']['bodytop']['global'] != '') {
    $output_string .= $thfr_css['codeinserts']['bodytop']['global'];
  }
  if ($thfr_css['codeinserts']['bodytop'][$thisTemplate . '-custom'] == 'on' and $thfr_css['codeinserts']['bodytop'][$thisTemplate] != '') {
    $output_string .= $thfr_css['codeinserts']['bodytop'][$thisTemplate];
  }

  return $output_string;
}

function tf_create_php_file_bodytag($thisTemplate) {
  global $thfr_css;

  $output_string = '';
  if ($thfr_css['codeinserts']['bodytag'][$thisTemplate . '-custom'] == 'on' and $thfr_css['codeinserts']['bodytag'][$thisTemplate] != '') {
    $output_string .= $thfr_css['codeinserts']['bodytag'][$thisTemplate];
  } else if ($thfr_css['codeinserts']['bodytag'][$thisTemplate . '-global'] == 'on' and $thfr_css['codeinserts']['bodytag']['global'] != '') {
    $output_string .= $thfr_css['codeinserts']['bodytag']['global'];
  } else {
    $output_string .= '<body <?php body_class("' . $thisTemplate . '"); ?>>';
  }

  return $output_string;
}

function tf_create_php_file_header($thisTemplate) {
  $string = get_drop_areas($thisTemplate, 'header');

  return tf_common_template_header_section(array('body' => $string));
}

function tf_create_php_file_footer($thisTemplate) {
  $string = get_drop_areas($thisTemplate, 'footer');

  return tf_common_template_footer_section(array('body' => $string));
}

function tf_create_php_file_left($thisTemplate) {
  global $thfr_css;

  if ($thfr_css[$thisTemplate]['sidebarsleft'] > 1) {
    $col_left_width = str_replace('px', '', $thfr_css['left-left-width']) + str_replace('px', '', $thfr_css['left-right-width']) . 'px';
  } else {
    $col_left_width = $thfr_css['left-left-width'];
  }

  $aContents = array(
    'sidebarsleft' => $thfr_css[$thisTemplate]['sidebarsleft'],
    'td_left_style' => ' style="width:' . $col_left_width . ';"',
    'sidebar_left3' => '<?php dynamic_sidebar("LEFT-3"); ?>',
    'sidebar_left1' => '<?php dynamic_sidebar("LEFT-1"); ?>',
    'sidebar_left2' => '<?php dynamic_sidebar("LEFT-2"); ?>',
    'drop_area_beforeSidebarLeft' => get_drop_areas($thisTemplate, "beforeSidebarLeft"),
    'drop_area_afterSidebarLeft' => get_drop_areas($thisTemplate, "afterSidebarLeft"),
  );

  return tf_common_template_left_section($aContents);
}

function tf_create_php_file_right($thisTemplate) {
  global $thfr_css;

  if ($thfr_css[$thisTemplate]['sidebarsright'] > 1) {
    $col_right_width = str_replace('px', '', $thfr_css['right-left-width']) + str_replace('px', '', $thfr_css['right-right-width']) . 'px';
  } else {
    $col_right_width = $thfr_css['right-right-width'];
  }
  $aContents = array(
    'sidebarsright' => $thfr_css[$thisTemplate]['sidebarsright'],
    'td_right_style' => ' style="width:' . $col_right_width . ';"',
    'sidebar_right3' => '<?php dynamic_sidebar("RIGHT-3"); ?>',
    'sidebar_right1' => '<?php dynamic_sidebar("RIGHT-1"); ?>',
    'sidebar_right2' => '<?php dynamic_sidebar("RIGHT-2"); ?>',
    'drop_area_beforeSidebarRight' => get_drop_areas($thisTemplate, "beforeSidebarRight"),
    'drop_area_afterSidebarRight' => get_drop_areas($thisTemplate, "afterSidebarRight"),
  );

  return tf_common_template_right_section($aContents);
}

function tf_create_php_file_content($thisTemplate) {
  $string = '';
  $string .= get_drop_areas($thisTemplate, 'centerTop');
  $string .= '
<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
<article>
';
  $string .= tf_postcontainer_code($thisTemplate);
  $string .= '
</article>
  <?php endwhile; ?>';
  $string .= get_drop_areas($thisTemplate, 'centerBeforeComments');
  $string .= '
<?php if ( is_single() ) { comments_template(); } ?>
<?php else : ?>
  <h2 class="center">По вашему запросу ничего не найдено</h2>
  <p class="center">Извините, но запрашиваемая страница не найдена.</p>
  <?php get_search_form(); ?>
<?php endif; ?>
';
  $string .= '<div style="clear: both;"></div>' . get_drop_areas($thisTemplate, 'centerBottom');
  $aContents = array(
    'body' => $string,
  );

  return tf_common_template_content_section($aContents);
}

function get_drop_areas($thisTemplate, $area_name) {
  global $thfr_css;

  $aElements = tf_get_elements();
  $string = '';
  $i = 0;
  foreach ($thfr_css[$thisTemplate] as $sCurrAreaName => $sContent) {
    $sAreaName = $area_name . '-' . (string)$i;
    if ($sAreaName == $sCurrAreaName) {
      $string .= $aElements[$thfr_css[$thisTemplate][$sAreaName]];
      $i++;
    }
  }
  return "<!-- Drop Area $area_name START -->" . $string . "<!-- Drop Area $area_name END -->";
}

?>